package com.yst.util;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Objects;

public class BeanUtils {
    /**
     * 设置字段值
     * @param fieldName 字段名称
     * @param object 要设置的对象
     * @param value 值
     * @throws Exception 抛出的异常
     */
    public static void setFieldValue(String fieldName,Object object,Object value) throws Exception {
        try {
            try {
                Field field=object.getClass().getDeclaredField(fieldName);
                field.setAccessible(true);
                field.set(object, value);
                return;
            }
            catch (Exception e){
                //ignore
            }

            String methodName = "set" + fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1);
            object.getClass().getMethod(methodName,value.getClass()).invoke(object, value);
        }
        catch (Exception e){
            throw new Exception(String.format("dynamic set object field value fail! field:%s,class:%s",
                    fieldName,object.getClass().getName()));
        }
    }


    /**
     * 动态获取对象字估值
     * @param object 对象
     * @param fieldName 字段名
     * @param defaultValue 默认值
     * @return 将要获取的值
     */
    public static Object getFieldValue(Object object,String fieldName,Object defaultValue){
        try {
            Object result = null;
            try {
                Field field=object.getClass().getDeclaredField(fieldName);
                field.setAccessible(true);
                result=field.get(object);
                if (Objects.nonNull(result)) {
                    return result;
                }
                return defaultValue;
            }
            catch (Exception e){
               //ignore
                e.printStackTrace();
            }

            try {
                Field field=object.getClass().getDeclaredField(fieldName);
                result=field.get(object);
                if (Objects.nonNull(result)) {
                    return result;
                }
                return defaultValue;
            }
            catch (Exception e){
                //ignore
                e.printStackTrace();
            }

            String methodName = "get" + fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1);
            Method method=object.getClass().getMethod(methodName);
            result=method.invoke(object);

            if (Objects.nonNull(result))
            {
                return result;
            }

            return defaultValue;
        }
        catch (Exception e){
            return defaultValue;
        }
    }

    /**
     * 获取字段的类型
     * @param object 对象
     * @param fieldName 字段名称
     * @param defaultClass 默认字段类型
     * @return 获取字段类型
     */
    public static Class<?> getFieldClass(Object object, String fieldName, Class<?> defaultClass){
        return getFieldClass(object.getClass(),fieldName,defaultClass);
    }

    /**
     * 获取字段的类
     * @param clazz 类型
     * @param fieldName 字段名称
     * @param defaultClass 默认类
     * @return 字段类
     */
    public static Class<?> getFieldClass(Class<?> clazz, String fieldName, Class<?> defaultClass){
        try {
            try {
                Field field=clazz.getField(fieldName);
                return field.getType();
            }
            catch (Exception e){
                //ignore
            }

            try {
                String methodName = "get" + fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1);
                Method method = clazz.getMethod(methodName);
                return method.getReturnType();
            }
            catch (Exception e){
                //ignore
            }

            return defaultClass;
        }
        catch (Exception e){
            return defaultClass;
        }
    }

}
